package com.baizhi.action;

import com.baizhi.common.Constants;
import com.baizhi.entity.Auction;
import com.baizhi.service.AuctionService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@Scope("prototype")
public class AuctionAction {
    private AuctionService auctionService;
    @Autowired
    public void setAuctionService(AuctionService auctionService) {
        this.auctionService = auctionService;
    }

    private List<Auction> list;
    private Integer totalPage;
    private Integer pageSize=3;
    private Integer pageNum = 1;
    private Auction auction;
    private File cover;
    private String coverFileName;

    public List<Auction> getList() {
        return list;
    }

    public void setList(List<Auction> list) {
        this.list = list;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Auction getAuction() {
        return auction;
    }

    public void setAuction(Auction auction) {
        this.auction = auction;
    }

    public File getCover() {
        return cover;
    }

    public void setCover(File cover) {
        this.cover = cover;
    }

    public String getCoverFileName() {
        return coverFileName;
    }

    public void setCoverFileName(String coverFileName) {
        this.coverFileName = coverFileName;
    }

    public String showAuction(){
        String auctionName = null;
        String auctionDesc=null;
        Date auctionStartTime=null;
        Date auctionEndTime = null;
        Double auctionPrice=null;
        if(auction!=null){
            auctionName=auction.getAuctionName();
            auctionDesc=auction.getAuctionDesc();
            auctionStartTime= auction.getAuctionStartTime();
            auctionEndTime=auction.getAuctionEndTime();
            auctionPrice=auction.getAuctionStartPrice();
        }
        PageInfo<Auction> pageInfo = auctionService.selectAll(auctionName,auctionDesc ,auctionStartTime ,
                auctionEndTime,auctionPrice , pageNum, pageSize);
        list = pageInfo.getList();
        totalPage = pageInfo.getPages();
        return "showAuctionOk";
    }
    public String addAuction(){
        try {
            FileUtils.copyFile(cover,new File(Constants.IMG_PATH, UUID.randomUUID().toString()+coverFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        auction.setAuctionPic(UUID.randomUUID().toString()+coverFileName);
        int i = auctionService.insertAuction(auction);
        return "success";
    }

    public String getOneAuction(){
        auction = auctionService.selectById(auction.getAuctionId());
        System.out.println("更新前通过ID获得一个auction"+auction);
        return "updateJSP";
    }
    public String updateAuction(){
        System.out.println("更新之前的拍卖品对象"+auction);
        String realCoverFileName = UUID.randomUUID().toString()+coverFileName;
        if(cover!=null){
            try {
                FileUtils.copyFile(cover,new File(Constants.IMG_PATH,realCoverFileName ));
            } catch (IOException e) {
                e.printStackTrace();
            }
            auction.setAuctionPic(realCoverFileName);
        }
        int i = auctionService.updateAuction(auction);
        return "success";
    }
    public String removeAuction(){
        int i = auctionService.deleteAuction(auction.getAuctionId());
        return "success";
    }
    //通过一个auction的ID  进入竞拍页面
    public String selectAuction(){
        auction = auctionService.selectById(auction.getAuctionId());
        return "selectAuctionJSP";
    }
}
